<?php

	require_once(dirname(__FILE__)."/../core/safeParam.php");

	if( (isset($_GET['function'])) && (isset($_GET['cid'])) ){
		$function = $_GET['function'];
		$cid      = $_GET['cid'];
	}

	switch ($function) {
		default:
			if (!isset($cid)) {
				$results = $database->select("SELECT * FROM news WHERE rid = " . ARID . " order by date ");
      	
      		echo '
      		<div class="tituloOpcionesAdmin">
					<img src="images/icons/newspaper.png">
					Noticias
				</div>
				
				<div class="descripcionOpcionesAdmin">
					Herramienta para informar novedades, por Ej: Promociones, nuevas sucursales, nuevos productos, etc.
					<br />
					Ideal ya que no se hace necesario tener que modificar la página principal.
				</div>
				
				<div class="volverOpcionesEditMenu">
					<a href="index.php?edit=news&function=add">
						<img src="add.png">
						Agregar Noticia
					</a>
				</div>
					 			     		 
      		<table border="0" class="table">
	      		<tr>
						<th>Fecha</th>
						<th>Título</th>
						<th>Acción</th>
					</tr>';
      	
 				if( $results[0]['content']  != ''){
 					for ($i=0; $i< count($results); $i++) {
 						echo '
 						<tr>
							<td>
								'. $results[$i]['date'] .'
							</td>
							
							<td>
								<a href="index.php?edit=news&function=edit&cid='. $results[$i]['id'] .'">
									'. $results[$i]['title'] .'
								</a>
							</td>
						
							<td>
								<a href="index.php?edit=news&function=edit&cid='. $results[$i]['id'] .'">
									<img src="application_edit.png" border="0"> Editar
								</a>
								 
								<a href="../functions/news.php?function=delete&id='. $results[$i]['id'] .'">
									<img src="delete.png" border="0"> Borrar
								</a> 
			 				</td>
						</tr>';
      			} 
 				}     	
				
				echo '</table>';
    		} else {
    			$results = $database->select("SELECT * FROM news where id = $cid");
?>

<script type="text/javascript" src="js/tinyMce/tiny_mce.js"></script>
		
		<script type="text/javascript">
			tinyMCE.init({
				// General options
				language : "es", 
      		mode : "textareas",
      		theme : "advanced",
      		
      		plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

      		// Theme options
      		theme_advanced_buttons1 : "template, preview",
      		theme_advanced_buttons2 : "newdocument, save,|,undo, redo,|,cut, copy, paste,|styleselect,formatselect,fontselect,fontsizeselect",
      		theme_advanced_buttons3 : "bold,italic,underline,strikethroughcut,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,insertdate,inserttime,|,forecolor,backcolor",
      		theme_advanced_buttons4 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|",
      		theme_advanced_buttons5 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,blockquote,pagebreak,|,insertfile,insertimage",
      		theme_advanced_toolbar_location : "top",
      		theme_advanced_toolbar_align : "left",
      		theme_advanced_statusbar_location : "bottom",
      		theme_advanced_resizing : false,

      		// Skin options
      		skin : "o2k7",
      		skin_variant : "silver",

      		// Example content CSS (should be your site CSS)
      		content_css : "css/example.css",

      		// Drop lists for link/image/media/template dialogs
      		template_external_list_url : "js/tiniMce/plugins/template/jstemplate_list.js",
      		external_link_list_url : "js/link_list.js",
      		external_image_list_url : "js/image_list.js",
      		media_external_list_url : "js/media_list.js",

      		// Replace values for the template plugin
      		template_replace_values : {
      			username : "Some User",
      			staffid : "991234"
      		}
			});
		</script>


				<div class="tituloOpcionesAdmin">
					<img src="images/icons/newspaper.png">
					Editar Noticia
				</div>
					
				<div class="volverOpcionesEditMenu">
					<a href="index.php?edit=news">
						<img src="images/icons/arrowBack.png">
						Volver al menú de noticias
					</a>
				</div>
    			
    			<form action="../functions/news.php?function=edit" method="post">
					<input type="hidden" name="id" value="<?php echo $results[0]['id'];?>">
					
					<div class="contenedorConfigCat">
						
						<div class="celdaContenedorConfigMain">
							<div class="opcionConfigMain">
								Título de la Noticia:
								<input type="text" name="title"  style="width:200px" value="<?php echo $results[0]['title'];?>">
							</div>
						</div>

						<div class="celdaContenedorConfigMain">
							<div class="opcionConfigMain">
								Fecha de la Noticia:
								<input type="text" name="date" style="width:200px" value="<?php echo date("Y-m-d");?>">
							</div>
						</div>	
						
						<div class="celdaContenedorConfigMain">
							<div class="opcionConfigMain">
								Cuerpo de la Noticia:
								
								<div class="editorNoticias">
									<textarea name="content" ><?php echo $results[0]['content']; ?></textarea>
								</div>
								
							</div>
						</div>
					</div>
					
					<div class="botonConfigMain">
						<input type="submit" value="Guardar">
					</div>
				</form>
<?php
	}
	break;
	
	case "add":
?>
		<script type="text/javascript" src="js/tinyMce/tiny_mce.js"></script>
		
		<script type="text/javascript">
			tinyMCE.init({
				// General options
				language : "es", 
      		mode : "textareas",
      		theme : "advanced",
      		
      		plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

      		// Theme options
      		theme_advanced_buttons1 : "template, preview",
      		theme_advanced_buttons2 : "newdocument, save,|,undo, redo,|,cut, copy, paste,|styleselect,formatselect,fontselect,fontsizeselect",
      		theme_advanced_buttons3 : "bold,italic,underline,strikethroughcut,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,blockquote,|,insertdate,inserttime,|,forecolor,backcolor",
      		theme_advanced_buttons4 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|",
      		theme_advanced_buttons5 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,blockquote,pagebreak,|,insertfile,insertimage",
      		theme_advanced_toolbar_location : "top",
      		theme_advanced_toolbar_align : "left",
      		theme_advanced_statusbar_location : "bottom",
      		theme_advanced_resizing : false,

      		// Skin options
      		skin : "o2k7",
      		skin_variant : "silver",

      		// Example content CSS (should be your site CSS)
      		content_css : "css/example.css",

      		// Drop lists for link/image/media/template dialogs
      		template_external_list_url : "js/tiniMce/plugins/template/jstemplate_list.js",
      		external_link_list_url : "js/link_list.js",
      		external_image_list_url : "js/image_list.js",
      		media_external_list_url : "js/media_list.js",

      		// Replace values for the template plugin
      		template_replace_values : {
      			username : "Some User",
      			staffid : "991234"
      		}
			});
		</script>

      <div class="tituloOpcionesAdmin">
			<img src="images/icons/newspaper.png">
			Agregar Noticia
		</div>
				
		<div class="volverOpcionesEditMenu">
			<a href="index.php?edit=news">
				<img src="images/icons/arrowBack.png">
				Volver al menú de noticias
			</a>
		</div>

		<form action="../functions/news.php?function=add" method="post">  
			<input type="hidden" name="rid" value="<?php echo ARID;?>" />
	
			<div class="contenedorConfigCat">
					
				<div class="celdaContenedorConfigMain">
					<div class="opcionConfigMain">
						Título de la Noticia:
						<input type="text" name="title"  style="width:200px" value="<?php echo $results[0]['title'];?>">
					</div>
				</div>
				
				<div class="celdaContenedorConfigMain">
					<div class="opcionConfigMain">
						Fecha de la Noticia:
						<input type="text" name="date" style="width:200px" value="<?php echo date("Y-m-d");?>">
					</div>
				</div>
					
				<div class="celdaContenedorConfigMain">
					<div class="opcionConfigMain">
						Cuerpo de la Noticia:
						
						<div class="editorNoticias">
							<textarea name="content" ><?php echo $results[0]['content']; ?></textarea>
						</div>
						
					</div>
				</div>					
			</div>
			
			<div class="botonConfigMain">
				<input type="submit" value="Guardar">
			</div>			
		</form>

<?php
    break;
} 
?>
